package com.zhongshengnetwork.rightbe.letter.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.LetterConstant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.dbmodel.PeopledbModel;
import com.zhongshengnetwork.rightbe.dbservice.PeopledbService;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BlackPeopleActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private List<PeopledbModel> list = null;
    private ListView listview;
    private TopBarView topbar;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_black_people);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        listview=(ListView) findViewById(R.id.blackfriend_listview);
        initData();
        adapter=new MyAdapter();
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    public void initData(){
        list = new ArrayList<PeopledbModel>();
        List<PeopledbModel> l = PeopledbService.getData(CustomApplication.loginModel.getUid(), LetterConstant.sx_black_people,0,5000);
        if (l != null && l.size() > 0){
            list.addAll(l);
        }
    }

    private class MyAdapter extends BaseAdapter implements View.OnClickListener{
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return super.isEnabled(position);
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view=convertView;
            view= LayoutInflater.from(BlackPeopleActivity.this).inflate(R.layout.blackfriend_listview_item, null);
            ImageView header = (ImageView) view.findViewById(R.id.blackfriend_listview_item_header);
            Button state = (Button) view.findViewById(R.id.blackfriend_listview_item_state);
            state.setTag(position);
            state.setOnClickListener(this);
            TextView name = (TextView) view.findViewById(R.id.blackfriend_listview_item_name);
            PeopledbModel model = list.get(position);
            name.setText(model.getNickname());
            Glide.with(BlackPeopleActivity.this).load(model.getHeader()).transform(new CircleTransform(BlackPeopleActivity.this)).dontAnimate().into(header);
            return view;
        }

        public void onClick(View view){
            int position = (int)view.getTag();
            PeopledbModel model = list.get(position);
            changeFriendType(model);
        }

    }

    private void changeFriendType(final PeopledbModel model){
        if (model != null){

            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            map.put("userid", model.getFromuserid());
            map.put("state","0");
            String url = "miniapp/letter/"+"langappid"+"/black.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        PeopledbService.deleteById(model.getId());
                        list.remove(model);
                        adapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onError(String msg) {

                }
            });

        }
    }

}
